<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	// Make sure this file isn't being accessed directly
	if(!defined('IN_X7CHAT3')) return;
	
	// Check to make sure this user actually has permission to create a room
	if(!$ses->user->has_permission('CREATE_ROOM')){
		echo tpl_create_room::room_creation_denied();
		return;
	}
	
	$form = new x7form();
	
	$form->add_section(1,$GLOBALS['LANG']->lang_roommgmt->create_room,'');
	$form->add_field_text(1,'room_name',$GLOBALS['LANG']->lang_roommgmt->room_name);
	$form->add_field_text(1,'topic',$GLOBALS['LANG']->lang_roommgmt->topic,'',lib_input::ALLOWALL,$GLOBALS['LANG']->lang_roommgmt->topic_desc);
	$form->add_field_text(1,'greeting',$GLOBALS['LANG']->lang_roommgmt->greeting,'',lib_input::ALLOWALL,$GLOBALS['LANG']->lang_roommgmt->greeting_desc);
	$form->add_field_text(1,'max_users',$GLOBALS['LANG']->lang_roommgmt->max_users,'15',lib_input::NUMBER);
	
	$res = $form->get_results();
	if($res){
		
		$len = x7utf8::strlen($res['room_name']);
		if($len < $settings['roomname_min_length'])
			$form->add_field_error('room_name',$GLOBALS['LANG']->lang_roommgmt->too_short_roomname);
		elseif($len > $settings['roomname_max_length'])
			$form->add_field_error('room_name',$GLOBALS['LANG']->lang_roommgmt->too_long_roomname);
		else{
			$room = new lib_room($res['room_name'],'name');
			if($room->is_valid())
				$form->add_field_error('room_name',$GLOBALS['LANG']->lang_roommgmt->roomname_in_use);
		}
		
		if($res['max_users'] < 3)
			$res['max_users'] = 3;
			
		if(!$form->has_errors()){
			
			$rid = $GLOBALS['DATABASE']->table_next_id('rooms');
			$room = new lib_room($rid);
			$room->data_cache = array(
					'rid'=>$rid,
					'name'=>$res['room_name'],
					'topic'=>$res['topic'],
					'greeting'=>$res['greeting'],
					'password'=>'',
					'max_users'=>$res['max_users'],
					'perms'=>array('user'=>array(),'group'=>array(),'default'=>array()),
					'ban_data'=>array(),
					'log'=>false,
				);
			$room->commit_changes();
			
			$room->set_permission('SEND_REGULAR_MESSAGE',1,$ses->user);
			$room->set_permission('BECOME_INVISIBLE',1,$ses->user);
			$room->set_permission('BAN_KICK_USER',1,$ses->user);
			$room->set_permission('MUTE_USER',1,$ses->user);
			$room->set_permission('VIEW_IP',1,$ses->user);
			$room->set_permission('SET_ROOM_LOG',1,$ses->user);
			$room->set_permission('VIEW_ROOM_LOG',1,$ses->user);
			$room->set_permission('SEE_INVISIBLE',1,$ses->user);
			$room->set_permission('EDIT_ROOM_ACCESS',1,$ses->user);
			$room->set_permission('EDIT_ROOM_PERMS',1,$ses->user);
			$room->set_permission('EDIT_ROOM_SETTINGS',1,$ses->user);
			$room->set_permission('PASSWORD_CORRECT',1,$ses->user);
			$room->set_permission('ENTER_FULL_ROOM',1,$ses->user);
			$room->set_permission('BAN_IMMUNITY',1,$ses->user);
			
			$room->set_permission('ACCESS_ROOM',1);
			$room->set_permission('SEND_REGULAR_MESSAGE',1);
			$room->set_permission('PASSWORD_CORRECT',1);
			
			$room->commit_changes();
			
			echo tpl_create_room::room_created($rid,$res['room_name'],$res['expire']);
			return;
		}
	}
	
	echo tpl_create_room::create_room_form($form);
	return;
?>